package org.example.SystemDemo.core;

import lombok.Data;
import org.example.SystemDemo.enums.ResultEnum;

@Data
public class Result<T>{

    private Integer code;
    private boolean success;
    private String msg;
    private T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    private Result(Integer code, String msg , T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
        if (code == 200){
            this.success = true;
        }else {
            this.success = false;
        }
    }

    public static <T> Result<T> success(String msg,T data){
        return new Result<T>(ResultEnum.SUCCESS.getCode(),msg,data);
    }

    public static <T> Result<T> ok(T data){
        ResultEnum anEnum = ResultEnum.SUCCESS;
        return new Result<T>(anEnum.getCode(), anEnum.getMessage(), data);
    }

    public static <T> Result<T> success(String msg){
        return new Result<T>(ResultEnum.SUCCESS.getCode(),msg,null);
    }

    public static <T> Result<T> error(String msg,T data){
        return new Result<T>(ResultEnum.ERROR.getCode(),msg,data);
    }

    public static <T> Result<T> error(String msg){
        return new Result<T>(ResultEnum.ERROR.getCode(),msg,null);
    }

    public static <T> Result<T> custom(ResultEnum resultEnum,T data){
        return new Result<T>(resultEnum.getCode(),resultEnum.getMessage(),data);
    }

}
